package com.yufubao.api.controller.applet.product;


import com.yufubao.framework.annotation.SystemLog;
import com.yufubao.framework.constant.NotesConstant;
import com.yufubao.framework.result.PageUtils;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.model.dto.applet.product.AttrDto;
import com.yufubao.model.pojo.applet.product.AttrEntity;
import com.yufubao.model.pojo.applet.product.ProductAttrValueEntity;
import com.yufubao.service.service.applet.product.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author zhangzhan
 * @email 52194440@qq.com
 * @date 2024-08-28 23:00:26
 */
@Api("商品属性")
@RestController
@RequestMapping("/applet/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 批量修改spu属性/规格信息
     *
     * @param spuId
     * @param productAttrValueEntities
     * @return
     */
    @RequestMapping(value = "/update/{spuId}", method = RequestMethod.PUT)
    //@RequiresPermissions("gulimallproduct:attr:list")
    @SystemLog(info = "批量修改spu属性信息")
    @ApiOperation(value = "批量修改spu属性信息", notes = NotesConstant.DEV)
    public ResponseResult attrValueUpdate(@PathVariable("spuId") Long spuId,
                                          @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        attrService.attrValueUpdate(spuId, productAttrValueEntities);

        return ResponseResult.ok();
    }

    /**
     * 修改回显spu属性/规格信息
     */
    @RequestMapping(value = "/base/listforspu/{spuId}",method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attr:list")
    @SystemLog(info = "修改回显spu属性信息")
    @ApiOperation(value = "修改回显spu属性信息", notes = NotesConstant.DEV)
    public ResponseResult baseListforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntities = attrService.baseListforspu(spuId);

        return ResponseResult.okResult(productAttrValueEntities);
    }

    /**
     * 根据属性id查询属性详情
     */
    @RequestMapping(value = "/info/{attrId}", method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:attr:info")
    @SystemLog(info = "根据属性id查询属性详情")
    @ApiOperation(value = "根据属性id查询属性详情", notes = NotesConstant.DEV)
    public ResponseResult info(@PathVariable("attrId") Long attrId) {

        return ResponseResult.okResult(attrService.getByIdCorrelationPath(attrId));
    }

    /**
     * 修改或新增属性对应的属性分组关联关系
     */
    @RequestMapping(value = "/updateOrSaveDetailed",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attr:update")
    @SystemLog(info = "修改或新增属性对应的属性分组关联关系")
    @ApiOperation(value = "修改或新增属性对应的属性分组关联关系", notes = NotesConstant.DEV)
    public ResponseResult updateOrSaveDetailed(@RequestBody AttrDto dto) {
        attrService.updateOrSaveDetailed(dto);

        return ResponseResult.ok();
    }

    /**
     * 根据分类id查询详细属性规格参数列表
     */
    @RequestMapping(value = "/{attrType}/list/{catelogId}",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallproduct:attr:list")
    @SystemLog(info = "根据分类id查询详细属性规格参数列表")
    @ApiOperation(value = "根据分类id查询详细属性规格参数列表", notes = NotesConstant.DEV)
    public ResponseResult attrTypeList(@RequestBody Map<String, Object> params,
                                       //分类id
                                       @PathVariable("catelogId") Long catelogId,
                                       //属性类型
                                       @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.attrTypeList(params, catelogId, attrType);

        return ResponseResult.okResultPage("page", page);
    }

    /**
     * 商品属性保存
     */
    @RequestMapping(value = "/saveDetailed",method = RequestMethod.POST)
    @SystemLog(info = "商品属性保存")
    @ApiOperation(value = "商品属性保存", notes = NotesConstant.DEV)
    public ResponseResult saveDetailed(@RequestBody AttrDto attr) {
        attrService.saveDetailed(attr);

        return ResponseResult.ok();
    }


    /**
     * 列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "列表")
    @SystemLog(info = "商品属性列表")
    public ResponseResult list(@RequestBody Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return ResponseResult.okResultPage("page", page);
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "修改")
    @SystemLog(info = "商品属性修改")
    public ResponseResult update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);

        return ResponseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除")
    @SystemLog(info = "商品属性删除")
    public ResponseResult delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return ResponseResult.ok();
    }

}
